package com.qishon.pc.domain.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/9.
 */
@Slf4j
public class CutOutUtil {
    @Value("${run.path}")
    private String runPath;
//        public void main(String str[]) throws IOException{
//            String CmdPath = runPath+"\\"+"11.pstools\\cutout.exe";
//            String ChannelEdgeValue = "60";
//            String path = runPath+"\\"+"02.Render\\J17001";
//            String savepath = runPath+"\\"+"03.Cutout";
//            PsTools(CmdPath,ChannelEdgeValue,path,savepath);
//        }

        public static void PsTools(String CmdPath,String ChannelEdgeValue,String path,String savepath) throws IOException{
            List<String> OriginPath = new ArrayList<String>();
            List<String> ChannelPath = new ArrayList<String>();
            FindFolder(path,OriginPath,ChannelPath);
            PsToolsCmd(CmdPath,ChannelEdgeValue,OriginPath,ChannelPath,savepath);
        }

        public static void FindFolder(String path,List<String> OriginPath,List<String> ChannelPath){
            File f = new File(path);
            File[] files =f.listFiles();
            if (files != null){
                for(File file:files){
                    if(file.isDirectory()){
                        int colorFlag = file.getName().toString().indexOf("color");
                        int alphaFlag = file.getName().toString().indexOf("alpha");
                        if(colorFlag != -1){
                            OriginPath.add(file.getPath().toString());
                        }
                        if(alphaFlag != -1){
                            ChannelPath.add(file.getPath().toString());
                        }
                    }
                }
            }
        }
        public static void PsToolsCmd(String CmdPath, String ChannelEdgeValue,List<String> OriginPath,List<String> ChannelPath, String SavePath) throws IOException {
            String OriginImagePath;
            String ChannelImagePath;
            String SaveImagePath;
            String match;
            for(int i = 0; i < OriginPath.size(); i++){
                OriginImagePath = OriginPath.get(i);

                String division[] = OriginImagePath.split("\\\\");
                match = division[division.length-1];
                String model = division[division.length-2];
                String division_name[] = match.split("_");
                match = division_name[0];
                for(int k = 1;k < division_name.length-1; k++){
                    match += "_";
                    match += division_name[k];
                }
//	    	System.out.println(match);
                for(int j = 0; j < ChannelPath.size(); j++){
                    if(ChannelPath.get(j).indexOf(match) != -1){
                        ChannelImagePath = ChannelPath.get(j);
                        SaveImagePath = SavePath + "\\" + model + "\\" + match;
                        File dir = new File(SaveImagePath);
                        if(!dir.exists()){
                            dir.mkdirs();
                        }
                        log.info("PsToolsCmd:CmdPath: "+CmdPath);
                        log.info("PsToolsCmd:OriginImagePath: "+OriginImagePath);
                        log.info("PsToolsCmd:ChannelImagePath: "+ChannelImagePath);
                        log.info("PsToolsCmd:SaveImagePath: "+SaveImagePath);
                        log.info("PsToolsCmd: "+CmdPath+" "+OriginImagePath+" "+ChannelImagePath+" "+SaveImagePath);
                        String commons[] = {CmdPath,ChannelEdgeValue,OriginImagePath,ChannelImagePath,SaveImagePath};
                        Process child = Runtime.getRuntime().exec(commons);
                        BufferedReader buf = new BufferedReader(new InputStreamReader(child.getErrorStream()));
                        String str = "";
                        while((str = buf.readLine())!= null)
                        {
                            System.out.println(str);
                        }
                        break;
                    }
                }
            }
        }
}
